<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Tutorial: bind_back() and More Complicated View Adaptors</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../stl_interfaces.html" title="Chapter 36. Boost.STLInterfaces">
<link rel="prev" href="tutorial___closure__and__adaptor_.html" title="Tutorial: closure and adaptor">
<link rel="next" href="examples.html" title="Examples">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="tutorial___closure__and__adaptor_.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../stl_interfaces.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="examples.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_stlinterfaces.tutorial___bind_back____and_more_complicated_view_adaptors"></a><a class="link" href="tutorial___bind_back____and_more_complicated_view_adaptors.html" title="Tutorial: bind_back() and More Complicated View Adaptors">Tutorial:
    <code class="computeroutput"><span class="identifier">bind_back</span><span class="special">()</span></code>
    and More Complicated View Adaptors</a>
</h2></div></div></div>
<p>
      As we saw in the previous section, you can use <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">stl_interfaces</span><span class="special">::</span><span class="identifier">bind_back</span><span class="special">()</span></code> in combination with <code class="computeroutput"><span class="identifier">closure</span></code>
      to make closure objects that use the pipe syntax. <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">bind_back</span><span class="special">()</span></code> is an addition to C++23 that is designed
      to make constructing view adaptors easier.
    </p>
<p>
      In some cases, you might have a view adaptor for which <code class="computeroutput"><span class="identifier">adaptor</span></code>
      is too simple. For instance, let's say you have a view adaptor <code class="computeroutput"><span class="identifier">foo</span></code> that can be parameterized in more than
      one way. Perhaps <code class="computeroutput"><span class="identifier">range</span> <span class="special">|</span>
      <span class="identifier">foo</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">)</span></code> is a valid use, but so is <code class="computeroutput"><span class="identifier">range</span>
      <span class="special">|</span> <span class="identifier">foo</span><span class="special">(</span><span class="identifier">b</span><span class="special">,</span>
      <span class="identifier">c</span><span class="special">,</span> <span class="identifier">d</span><span class="special">)</span></code>. If this
      is the case, <code class="computeroutput"><span class="identifier">adaptor</span></code>'s all-but-one
      argument binding is no help.
    </p>
<p>
      If you have a view adaptor like this, you should re-introduce an <code class="computeroutput"><span class="identifier">impl</span></code> class like we saw in the <code class="computeroutput"><span class="identifier">all</span><span class="special">()</span></code> example,
      and construct <code class="computeroutput"><span class="identifier">closure</span></code>s from
      <code class="computeroutput"><span class="identifier">bind_back</span><span class="special">()</span></code>
      expressions as needed:
    </p>
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">R</span><span class="special">&gt;</span>
<span class="keyword">struct</span> <span class="identifier">foo_view</span>
<span class="special">{</span>
    <span class="comment">// etc.</span>
    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">R2</span><span class="special">&gt;</span>
    <span class="identifier">requires</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">is_same_v</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">remove_reference_t</span><span class="special">&lt;</span><span class="identifier">R2</span><span class="special">&gt;,</span> <span class="identifier">R</span><span class="special">&gt;</span>
    <span class="identifier">foo_view</span><span class="special">(</span><span class="identifier">R2</span> <span class="special">&amp;&amp;,</span> <span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">);</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">R2</span><span class="special">&gt;</span>
    <span class="identifier">requires</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">is_same_v</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">remove_reference_t</span><span class="special">&lt;</span><span class="identifier">R2</span><span class="special">&gt;,</span> <span class="identifier">R</span><span class="special">&gt;</span>
    <span class="identifier">foo_view</span><span class="special">(</span><span class="identifier">R2</span> <span class="special">&amp;&amp;,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">,</span> <span class="keyword">double</span><span class="special">);</span>
    <span class="comment">// etc.</span>
<span class="special">};</span>

<span class="keyword">struct</span> <span class="identifier">foo_impl</span>
<span class="special">{</span>
    <span class="comment">// These just directly construct the view from the full set of arguments.</span>
    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">R</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">R</span> <span class="special">&amp;&amp;</span> <span class="identifier">r</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">s</span><span class="special">)</span> <span class="keyword">const</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">foo_view</span><span class="special">((</span><span class="identifier">R</span> <span class="special">&amp;&amp;)</span><span class="identifier">r</span><span class="special">,</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">s</span><span class="special">));</span>
    <span class="special">}</span>
    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">R</span><span class="special">&gt;</span>
    <span class="keyword">auto</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">R</span> <span class="special">&amp;&amp;</span> <span class="identifier">r</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">s</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">i</span><span class="special">,</span> <span class="keyword">double</span> <span class="identifier">d</span><span class="special">)</span> <span class="keyword">const</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">foo_view</span><span class="special">((</span><span class="identifier">R</span> <span class="special">&amp;&amp;)</span><span class="identifier">r</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">s</span><span class="special">),</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">d</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">// This one binds *this, i, and s into a bind-expression (which is an</span>
    <span class="comment">// invocable object), and then uses that invocable to construct a</span>
    <span class="comment">// closure.  When the closure is called with a range r, it forwards that</span>
    <span class="comment">// range arg to the bind-expression, which calls (*this)(r, i,</span>
    <span class="comment">// std::move(s)).</span>
    <span class="keyword">auto</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">int</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">s</span><span class="special">)</span> <span class="keyword">const</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">stl_interfaces</span><span class="special">::</span><span class="identifier">closure</span><span class="special">(</span>
            <span class="identifier">boost</span><span class="special">::</span><span class="identifier">stl_interfaces</span><span class="special">::</span><span class="identifier">bind_back</span><span class="special">(*</span><span class="keyword">this</span><span class="special">,</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">s</span><span class="special">)));</span>
    <span class="special">}</span>

    <span class="comment">// Works just like the overload immediately above, but with a</span>
    <span class="comment">// different set of parameters.</span>
    <span class="keyword">auto</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">s</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">i</span><span class="special">,</span> <span class="keyword">double</span> <span class="identifier">d</span><span class="special">)</span> <span class="keyword">const</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">stl_interfaces</span><span class="special">::</span><span class="identifier">closure</span><span class="special">(</span>
            <span class="identifier">boost</span><span class="special">::</span><span class="identifier">stl_interfaces</span><span class="special">::</span><span class="identifier">bind_back</span><span class="special">(*</span><span class="keyword">this</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">s</span><span class="special">),</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">d</span><span class="special">));</span>
    <span class="special">}</span>
<span class="special">};</span>

<span class="keyword">inline</span> <span class="keyword">constexpr</span> <span class="identifier">foo_impl</span> <span class="identifier">foo</span><span class="special">;</span>
</pre>
<p>
      With these definitions, you can use <code class="computeroutput"><span class="identifier">foo</span></code>
      as we discussed earlier; <code class="computeroutput"><span class="identifier">range</span> <span class="special">|</span> <span class="identifier">foo</span><span class="special">(</span><span class="number">42</span><span class="special">,</span> <span class="string">"bar"</span><span class="special">)</span></code> and <code class="computeroutput"><span class="identifier">range</span>
      <span class="special">|</span> <span class="identifier">foo</span><span class="special">(</span><span class="string">"bar"</span><span class="special">,</span> <span class="number">42</span><span class="special">,</span>
      <span class="number">3.14</span><span class="special">)</span></code>
      are both well-formed, and do what you'd expect.
    </p>
</div>
<div class="copyright-footer">Copyright © 2019 T. Zachary Laine<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="tutorial___closure__and__adaptor_.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../stl_interfaces.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="examples.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
